package cn.wl.pchart.controller;

import cn.wl.pchart.entity.Message;
import cn.wl.pchart.service.CommunicationService;
import cn.wl.pchart.tools.OperatorCallback;
import cn.wl.pchart.tools.OperatorCore;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

// 消息控制器类

// 避免跨域拦截
@CrossOrigin
// 控制器
@RestController
@RequestMapping("/message")
public class MessageController {

    @Autowired
    CommunicationService communicationService;

    // 获取某两个用户某个time之前的聊天记录
    @RequestMapping(value = "/getMessage", produces = {"application/json;charset=utf-8"}, method = RequestMethod.POST)
    public JSONObject getMessage(@RequestBody String data) {

        /*
        {
        "user1": "用户1id",
        "user2": "用户2id",
        "time": "上一个时间，为空则自动设置为当前时间",
        "num": "获取消息条数，为空则自动设置为30条"
        }
        */

        return OperatorCore.handle(data, new OperatorCallback() {
            @Override
            public String call() throws Exception {
                String user1 = null;
                String user2 = null;
                String time = null;
                String num = null;

                try {
                    // 获取user1
                    user1 = rawJson.getString("user1");
                    // 获取user2
                    user2 = rawJson.getString("user2");
                    // 获取time
                    time = rawJson.getString("time");
                    // 获取num
                    num = rawJson.getString("num");
                } catch (Exception e) {
                    System.out.println("从JSON对象中获取数据失败！");
                    e.printStackTrace();
                    returnJson.put("code", 1);
                    returnJson.put("msg", "从JSON对象中获取数据失败！请检查数据格式：user1、user2、time、num");
                    return returnJson.toString();
                }

                try {
                    if (user1 != null && user2 != null) {

                        if (time == null) {
                            time = "CURRENT_TIMESTAMP()";
                        }
                        if (time.equals("") || time.equals("null")) {
                            time = "CURRENT_TIMESTAMP()";
                        }
                        if (num == null) {
                            num = "30";
                        }
                        if (num.equals("") || num.equals("null")) {
                            num = "30";
                        }

                        List<Message> messages = communicationService
                                .getMessage(user1, user2, time, Integer.valueOf(num));
                        returnJson.put("code", 0);
                        returnJson.put("msg", "成功获取信息！");
                        returnJson.put("data", messages);
                        return returnJson.toString();
                    } else {
                        returnJson.put("code", 3);
                        returnJson.put("msg", "两个用户id不允许为空！");
                        return returnJson.toString();
                    }
                } catch (Exception e) {
                    System.out.println("获取消息失败！");
                    e.printStackTrace();
                    returnJson.put("code", 2);
                    returnJson.put("msg", "获取消息失败！");
                    return returnJson.toString();
                }
            }
        });
    }

    // 存储一条message
    @RequestMapping(value = "/saveMessage", produces = {"application/json;charset=utf-8"}, method = RequestMethod.POST)
    public JSONObject saveMessage(@RequestBody String data) {
        return OperatorCore.handle(data, new OperatorCallback() {

        /*
        {
        "from": "消息来源用户",
        "to": "消息目标用户",
        "data": "消息内容"
        }
        */

            @Override
            public String call() throws Exception {
                String from = null;
                String to = null;
                String data = null;

                try {
                    // 获取消息来源的id
                    from = rawJson.getString("from");
                    // 获取目标用户id
                    to = rawJson.getString("to");
                    // 获取消息内容
                    data = rawJson.getString("data");
                } catch (Exception e) {
                    System.out.println("从JSON对象中获取数据失败！");
                    e.printStackTrace();
                    returnJson.put("code", 1);
                    returnJson.put("msg", "从JSON对象中获取数据失败！请检查数据格式：from、to、data");
                    return returnJson.toString();
                }

                try {
                    if (from != null && to != null) {

                        communicationService.saveMessage(from, to, data);
                        returnJson.put("code", 0);
                        returnJson.put("msg", "成功保存聊天记录！");
                        return returnJson.toString();
                    } else {
                        returnJson.put("code", 3);
                        returnJson.put("msg", "两个用户id不允许为空！");
                        return returnJson.toString();
                    }
                } catch (Exception e) {
                    System.out.println("保存聊天记录失败！");
                    e.printStackTrace();
                    returnJson.put("code", 2);
                    returnJson.put("msg", "保存聊天记录失败！");
                    return returnJson.toString();
                }
            }
        });
    }

    // 删除一条message
    @RequestMapping(value = "/deleteMessage", produces = {"application/json;charset=utf-8"}, method = RequestMethod.POST)
    public JSONObject deleteMessage(@RequestBody String data) {
        return OperatorCore.handle(data, new OperatorCallback() {

        /*
        {
        "mid": "聊天消息对应的mid",
        }
        */

            @Override
            public String call() throws Exception {
                String mid = null;

                try {
                    // 获取消息来源的id
                    mid = rawJson.getString("mid");
                } catch (Exception e) {
                    System.out.println("从JSON对象中获取数据失败！");
                    e.printStackTrace();
                    returnJson.put("code", 1);
                    returnJson.put("msg", "从JSON对象中获取数据失败！请检查数据格式：mid");
                    return returnJson.toString();
                }

                try {
                    communicationService.deleteMessage(mid);
                    returnJson.put("code", 0);
                    returnJson.put("msg", "成功删除聊天记录！");
                    return returnJson.toString();
                } catch (Exception e) {
                    System.out.println("删除聊天记录失败！");
                    e.printStackTrace();
                    returnJson.put("code", 2);
                    returnJson.put("msg", "删除聊天记录失败！");
                    return returnJson.toString();
                }
            }
        });
    }
}